﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository;
using Model.DomainModel;
using Utility.Enum;

namespace Infrastructure.Repository
{
    /// <summary>
    /// 流程消息仓储接口实现
    /// </summary>
    public class FlowMessageRepository : Repository<FlowMessage>, IFlowMessageRepository
    {
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="flowMessages">流程信息</param>
        public void BulkInsert(List<FlowMessage> flowMessages)
        {
            string sql = "  insert into FlowMessage (MessageId ,MessageTitle,MessageType,MessageContent,MessageRecipient ,MessageSender ,MessageState,FlowApplyId ,NodeId ,NodeName ,IsBrowse,BrowseTime,IsHandled,HandledTime,HandledType,HandledState ,CreateTime,CreateUserKey) values(@MessageId ,@MessageTitle,@MessageType,@MessageContent,@MessageRecipient ,@MessageSender ,@MessageState,@FlowApplyId ,@NodeId ,@NodeName ,@IsBrowse,@BrowseTime,@IsHandled,@HandledTime,@HandledType,@HandledState ,@CreateTime,@CreateUserKey) ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, flowMessages);
        }

        /// <summary>
        /// 获取流程信息数据
        /// </summary>
        /// <param name="flowApplyId">流程申请id</param>
        /// <param name="nodeId">节点id</param>
        /// <returns></returns>
        public List<FlowMessage> GetFlowMessageData(string flowApplyId, string nodeId)
        {
            string sql = @"   select * from FlowMessage where FlowApplyId=@FlowApplyId and NodeId=@NodeId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<FlowMessage>(sql, new { FlowApplyId = flowApplyId, NodeId = nodeId }).ToList();
        }

        /// <summary>
        /// 获取流程信息数据
        /// </summary>
        /// <param name="messageRecipient">消息接收人id</param>
        /// <returns></returns>
        public List<FlowMessage> GetFlowMessageByUserId(string messageRecipient)
        {
            string sql = @" select * from FlowMessage where MessageRecipient=@MessageRecipient ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<FlowMessage>(sql, new { MessageRecipient = messageRecipient }).ToList();
        }

        /// <summary>
        /// 更新流程信息
        /// </summary>
        /// <param name="flowMessage">消息数据</param>
        public void UpdateFlowMessage(FlowMessage flowMessage)
        {
            string sql = "  update FlowMessage set IsHandled=@IsHandled,HandledTime=@HandledTime,HandledType=@HandledType,HandledState=@HandledState,MessageType=@MessageType  where MessageRecipient = @MessageRecipient and FlowApplyId=@FlowApplyId and NodeId=@NodeId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { flowMessage.MessageRecipient, flowMessage.FlowApplyId, flowMessage.NodeId, flowMessage.IsHandled, flowMessage.HandledTime, flowMessage.HandledType, flowMessage.HandledState, MessageType = (int)MessageTypeEnum.ExpireRemind });
        }

        /// <summary>
        /// 获取未处理的流程消息
        /// </summary>
        /// <param name="flowMessage">消息数据</param>
        public List<FlowMessage> GetUnHandledFlowMessage(FlowMessage flowMessage)
        {
            string sql = "   select * from FlowMessage  where MessageRecipient = @MessageRecipient and FlowApplyId=@FlowApplyId and NodeId=@NodeId and IsHandled=0 and MessageType=@MessageType";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<FlowMessage>(sql, new { flowMessage.MessageRecipient, flowMessage.FlowApplyId, flowMessage.NodeId, MessageType = (int)MessageTypeEnum.ExpireRemind }).ToList();
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="flowApplyId">申请id</param>
        /// <param name="recipientKeys">接收者key集合</param>
        /// <param name="messageSender">发送者key</param>
        /// <param name="messageType">消息类型</param>
        /// <returns></returns>
        public List<FlowMessage> GetCarbonCopyFlowMessage(string flowApplyId, List<string> recipientKeys, string messageSender, MessageTypeEnum messageType)
        {
            string sql = "  select * from FlowMessage  where MessageRecipient in @MessageRecipient and FlowApplyId=@FlowApplyId  and MessageType=@MessageType and MessageSender=@MessageSender";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<FlowMessage>(sql, new { MessageRecipient = recipientKeys, FlowApplyId = flowApplyId, MessageSender = messageSender, MessageType = (int)messageType }).ToList();
        }
    }
}
